﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class BasicBase_SoldierArchive_SoldierView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Soldier soldier = new Soldier();
            SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(Request["ID"]);
            bool enableViewed = false;
            string AccountDepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203001"))
            {
                enableViewed = true;
            }
            else
            {
                if ((Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203006")))
                    enableViewed = AccountDepartmentID.Substring(0, 4) == soldierInfo.DepartmentID.Substring(0, 4);
                else
                    enableViewed = AccountDepartmentID == soldierInfo.DepartmentID;
            }
            if (enableViewed)
            {
                //lblTitle.Text = "兵员详细信息";
                SoldierName.Text = soldierInfo.SoldierName;
                CardID.Text = soldierInfo.CardID;
                Sex.Text = soldierInfo.Sex;
                imgPhoto.ImageUrl = soldierInfo.Picture == "" ? "~/Images/NoPhoto.png" : "../../" + soldierInfo.Picture;
                NativeName.Text = soldierInfo.NativeName;
                NationName.Text = soldierInfo.NationName;
                BackgroundName.Text = soldierInfo.BackgroundName;
                Marriage.Text = soldierInfo.Marriage;
                CultureName.Text = soldierInfo.CultureName;
                ReligionName.Text = soldierInfo.ReligionName;
                DepartmentName.Text = soldierInfo.DepartmentName;
                Birthday.Text = soldierInfo.Birthday.Year == 9999 ? "--" : soldierInfo.Birthday.ToString("yyyy-MM-dd");
                JoinDate.Text = soldierInfo.Birthday.Year == 9999 ? "--" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
                YouthDate.Text = soldierInfo.Birthday.Year == 9999 ? "--" : soldierInfo.YouthTime.ToString("yyyy-MM-dd");
                PartyDate.Text = soldierInfo.Birthday.Year == 9999 ? "--" : soldierInfo.PartyTime.ToString("yyyy-MM-dd");
                RankName.Text = soldierInfo.RankName;
                DutyName.Text = soldierInfo.DutyName;
                Speciality1Name.Text = soldierInfo.Speciality1Name;
                Speciality2Name.Text = soldierInfo.Speciality2Name;
                JoinPlace.Text = soldierInfo.JoinPlace;
                ConfirmID.Text = soldierInfo.ConfirmID;
                ChooseID.Text = soldierInfo.ChooseID;
                RegisterID.Text = soldierInfo.RegisterID;
                Height.Text = soldierInfo.Height == 0 ? "--" : soldierInfo.Height.ToString() + "cm";
                Weight.Text = soldierInfo.Weight == 0.0m ? "--" : soldierInfo.Weight.ToString() + "kg";
                SetUserName.Text = soldierInfo.SetUserName;
                SetTime.Text = soldierInfo.SetTime.ToString("yyyy-MM-dd");
                HomePlace.Text = soldierInfo.HomePlace;
                Hobby.Text = soldierInfo.Hobby;
                Political.Text = soldierInfo.Political;
                if (soldierInfo.IsDelete)
                {
                    btnRestore.Visible = true;
                    lblDelete.Visible = true;
                }
                BindTransferList();
            }
            else
            {
                lblTitle.Text = "您无权查看此兵员信息。";
                imgPhoto.ImageUrl = "~/Images/NoPhoto.png";
            }
        }
    }
    protected void btnRestore_Click(object sender, EventArgs e)
    {
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(Request["ID"]);
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203001") ||
            Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101") ||
            ((Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203008")) && soldierInfo.DepartmentID.Substring(0, 4) == Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4)) ||
            (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203009") && soldierInfo.DepartmentID == Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID))
        {
            if (soldier.Restore(soldierInfo.SoldierID, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName, DateTime.Now))
            {
                lblDelete.Text = "已恢复。";
                btnRestore.Visible = false;
            }
            else
                lblDelete.Text = "恢复失败。";
        }
        else
        {
            lblDelete.Text = "权限不足。";
            btnRestore.Visible = false;
        }

    }
    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (gvTransfer.PageIndex * gvTransfer.PageSize + e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

        }
    }

    private void BindTransferList()
    {
        gvTransfer.DataSource = TransferSoldier.GetTransferListBySoldierID(Request["ID"]);
        gvTransfer.DataBind();
    }

}
